package ru.niir.goodfil.audit.service;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import ru.niir.goodfil.audit.domain.*;
import ru.niir.goodfil.audit.util.Assert;

import java.util.Properties;

public class Configuration {
    private final String databaseName;

    public Configuration(String databaseName) {
        Assert.notNull(databaseName);

        this.databaseName = databaseName;
    }

    public SessionFactory getSessionFactory() {
        Properties props = new Properties();
        props.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
        props.put("hibernate.connection.driver_class", "org.h2.Driver");
        props.put("hibernate.connection.url", "jdbc:h2:tcp://localhost/home/sazonov/" + databaseName);
        props.put("hibernate.connection.username", "sa");
        props.put("hibernate.connection.password", "");
        props.put("hibernate.cache.provider_class", "org.hibernate.cache.NoCacheProvider");
        props.put("hibernate.hbm2ddl.auto", "update");
        props.put("hibernate.show_sql", "true");
        props.put("hibernate.transaction.factory_class", "org.hibernate.transaction.JDBCTransactionFactory");
        props.put("hibernate.current_session_context_class", "thread");

        AnnotationConfiguration configuration = new AnnotationConfiguration();
        configuration.setProperties(props);
        configuration.addAnnotatedClass(ClientC1NumChangeEvent.class);
        configuration.addAnnotatedClass(ClientDebtChangeEvent.class);
        configuration.addAnnotatedClass(ClientDeliveryInfoChangeEvent.class);
        configuration.addAnnotatedClass(ClientPriceColumnChangeEvent.class);
        configuration.addAnnotatedClass(ClientUIEvent.class);
        configuration.addAnnotatedClass(EntityEvent.class);
        configuration.addAnnotatedClass(EnviromentIntegrityEvent.class);
        configuration.addAnnotatedClass(ExceptionEvent.class);
        configuration.addAnnotatedClass(GoodUIEvent.class);
        configuration.addAnnotatedClass(LoginEvent.class);
        configuration.addAnnotatedClass(OrderUIBuildEvent.class);
        configuration.addAnnotatedClass(PasswordChangeEvent.class);
        configuration.addAnnotatedClass(PersonalPriceEvent.class);
        configuration.addAnnotatedClass(ReplacementUIEvent.class);
        configuration.addAnnotatedClass(SettingModificationEvent.class);
        configuration.addAnnotatedClass(UserUIEvent.class);
        configuration.addAnnotatedClass(OrderUIEvent.class);
        configuration.addAnnotatedClass(OrderItemUIEvent.class);
        return configuration.buildSessionFactory();
    }
}
